﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace nsGumController
{
    public partial class DockCommandBtn : DockContent
    {
        public DockCommandBtn()
        {
            InitializeComponent();
        }

        private CLog mLog = CGumController.mLog;

        private void EnQueueSend(ET_GPES_SENDMSG stSend)
        {
            CGumController.mDockSerialProtocol.EnQueueSend(stSend);
        }

        private void MoveTo(int coordinateX, int coordinateY)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_MOVETO;
            stSend.sendMsg = new byte[8];
            stSend.sendMsg[0] = (byte)(coordinateX & 0xff);
            stSend.sendMsg[1] = (byte)(coordinateX >> 8 & 0xff);
            stSend.sendMsg[2] = (byte)(coordinateX >> 16 & 0xff);
            stSend.sendMsg[3] = (byte)(coordinateX >> 24 & 0xff);

            stSend.sendMsg[4] = (byte)(coordinateY & 0xff);
            stSend.sendMsg[5] = (byte)(coordinateY >> 8 & 0xff);
            stSend.sendMsg[6] = (byte)(coordinateY >> 16 & 0xff);
            stSend.sendMsg[7] = (byte)(coordinateY >> 24 & 0xff);
            EnQueueSend(stSend);
        }

        private void C1Reverse(byte bt)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_C1;
            stSend.sendMsg = new byte[1] { bt };
            EnQueueSend(stSend);
        }

        private void C2Reverse(byte bt)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_C2;
            stSend.sendMsg = new byte[1] { bt };
            EnQueueSend(stSend);
        }

        private void C3Reverse(byte bt)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_C3;
            stSend.sendMsg = new byte[1] { bt };
            EnQueueSend(stSend);
        }

        private void ZReverse(byte bt)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_Cz;
            stSend.sendMsg = new byte[1] { bt };
            EnQueueSend(stSend);
        }

        private void GumSwitch(byte bt)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_GUMPOINTER;
            stSend.sendMsg = new byte[1] { bt };
            EnQueueSend(stSend);
        }

        private void MicroMove(byte direction, int steps)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_ADJUST;
            stSend.sendMsg = new byte[5];
            stSend.sendMsg[0] = direction;
            stSend.sendMsg[1] = (byte)(steps & 0xff);
            stSend.sendMsg[2] = (byte)(steps >> 8 & 0xff);
            stSend.sendMsg[3] = (byte)(steps >> 16 & 0xff);
            stSend.sendMsg[4] = (byte)(steps >> 24 & 0xff);
            EnQueueSend(stSend);
        }

        private void MotorLineMove(byte direction)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_LM;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = direction;
            EnQueueSend(stSend);
        }

        /// <summary>
        /// 传感器控制调试命令
        /// </summary>
        /// <param name="sensorIndex">从1开始</param>
        /// <param name="singal">1表示从0变1</param>
        private void SensorCtrl(int sensorIndex, int singal)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_SENSOR;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = (byte)((sensorIndex - 1) * 2 + (singal == 1 ? 0 : 1));
            EnQueueSend(stSend);
        }

        /// <summary>
        /// 设置电机移动到位标志
        /// </summary>
        /// <param name="xyz">1：x轴 2：y轴</param>
        private void SetInpFlag(byte xyz)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_INP;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = xyz;
            EnQueueSend(stSend);
        }

        /// <summary>
        /// 设置电机移动到Home点标志
        /// </summary>
        /// <param name="xyz">1：x轴 2：y轴</param>
        private void SetAtHomeFlag(byte xyz)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_ATHOME;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = xyz;
            EnQueueSend(stSend);
        }

        private void btnMoveTo_Click(object sender, EventArgs e)
        {
            double coordinateX = 0;
            double.TryParse(txtCoordinateXSet.Text, out coordinateX);
            double coordinateY = 0;
            double.TryParse(txtCoordinateYSet.Text, out coordinateY);
            MoveTo((int)(coordinateX), (int)(coordinateY));
        }

        private void btnC1Up_Click(object sender, EventArgs e)
        {
            C1Reverse(1);
        }

        private void btnC1Down_Click(object sender, EventArgs e)
        {
            C1Reverse(0);
        }

        private void btnC2Up_Click(object sender, EventArgs e)
        {
            C2Reverse(1);
        }

        private void btnC2Down_Click(object sender, EventArgs e)
        {
            C2Reverse(0);
        }

        private void btnC3Up_Click(object sender, EventArgs e)
        {
            C3Reverse(1);
        }

        private void btnC3Down_Click(object sender, EventArgs e)
        {
            C3Reverse(0);
        }

        private void btnZUp_Click(object sender, EventArgs e)
        {
            ZReverse(1);
        }

        private void btnZDown_Click(object sender, EventArgs e)
        {
            ZReverse(0);
        }

        private void btnGumOn_Click(object sender, EventArgs e)
        {
            GumSwitch(1);
        }

        private void btnGumOff_Click(object sender, EventArgs e)
        {
            GumSwitch(0);
        }

        private void btnSensor11_Click(object sender, EventArgs e)
        {
            SensorCtrl(1, 1);
        }

        private void btnSensor10_Click(object sender, EventArgs e)
        {
            SensorCtrl(1, 0);
        }

        private void btnSensor21_Click(object sender, EventArgs e)
        {
            SensorCtrl(2, 1);
        }

        private void btnSensor20_Click(object sender, EventArgs e)
        {
            SensorCtrl(2, 0);
        }

        private void btnSensor31_Click(object sender, EventArgs e)
        {
            SensorCtrl(3, 1);
        }

        private void btnSensor30_Click(object sender, EventArgs e)
        {
            SensorCtrl(3, 0);
        }

        private void btnMoveXPlus_Click(object sender, EventArgs e)
        {
            int moveSteps = 0;
            int.TryParse(txtMoveSteps.Text, out moveSteps);
            MicroMove(0, moveSteps);
        }

        private void btnMoveXMinus_Click(object sender, EventArgs e)
        {
            int moveSteps = 0;
            int.TryParse(txtMoveSteps.Text, out moveSteps);
            MicroMove(0, -moveSteps);
        }

        private void btnMoveYPlus_Click(object sender, EventArgs e)
        {
            int moveSteps = 0;
            int.TryParse(txtMoveSteps.Text, out moveSteps);
            MicroMove(1, moveSteps);
        }

        private void btnMoveYMinus_Click(object sender, EventArgs e)
        {
            int moveSteps = 0;
            int.TryParse(txtMoveSteps.Text, out moveSteps);
            MicroMove(1, -moveSteps);
        }

        private void btnMotorLinePos_Click(object sender, EventArgs e)
        {
            MotorLineMove(1);
        }

        private void btnMotorLineNeg_Click(object sender, EventArgs e)
        {
            MotorLineMove(2);
        }

        private void btnMotorLineStop_Click(object sender, EventArgs e)
        {
            MotorLineMove(0);
        }

        private void DockCommandBtn_KeyDown(object sender, KeyEventArgs e)
        {
            byte direction = 0;
            double moveSteps = 0;
            double.TryParse(txtMoveSteps.Text, out moveSteps);
            int iMoveSteps = (int)(moveSteps*10);
            if (e.KeyCode == Keys.Up)
            {
                direction = 1;
            }
            else if (e.KeyCode == Keys.Down)
            {
                direction = 1;
                iMoveSteps = -iMoveSteps;
            }
            else if (e.KeyCode == Keys.Left)
            {
                direction = 0;
                iMoveSteps = -iMoveSteps;
            }
            else if (e.KeyCode == Keys.Right)
            {
                direction = 0;
            }
            else
            {
                return;
            }

            MicroMove(direction, iMoveSteps);
        }

        /// <summary>
        /// 用于防止与微调功能的上下左右控制冲突
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Up)
                || (e.KeyCode == Keys.Down)
                    || (e.KeyCode == Keys.Left)
                    || (e.KeyCode == Keys.Right))
            {
                e.Handled = true;
            }
        }

        /// <summary>
        /// 发送重复做action次数的指令，用于调试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAciontRepeat_Click(object sender, EventArgs e)
        {
            UInt32 repeatTimes = 0;
            UInt32.TryParse(txtRepeatTimes.Text, out repeatTimes);

            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_REPEAT;
            stSend.sendMsg = new byte[4];
            stSend.sendMsg[0] = (byte)(repeatTimes & 0xff);
            stSend.sendMsg[1] = (byte)(repeatTimes >> 8 & 0xff);
            stSend.sendMsg[2] = (byte)(repeatTimes >> 16 & 0xff);
            stSend.sendMsg[3] = (byte)(repeatTimes >> 24 & 0xff);

            EnQueueSend(stSend);
        }

        private void btnGumDealy_Click(object sender, EventArgs e)
        {
            UInt32 gumDelay = 0;
            UInt32.TryParse(txtGumDelay.Text, out gumDelay);

            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_GUM_DELAY;
            stSend.sendMsg = new byte[4];
            stSend.sendMsg[0] = (byte)(gumDelay & 0xff);
            stSend.sendMsg[1] = (byte)(gumDelay >> 8 & 0xff);
            stSend.sendMsg[2] = (byte)(gumDelay >> 16 & 0xff);
            stSend.sendMsg[3] = (byte)(gumDelay >> 24 & 0xff);

            EnQueueSend(stSend);
        }

        private void btnInpX_Click(object sender, EventArgs e)
        {
            SetInpFlag(1);
        }

        private void btnInpY_Click(object sender, EventArgs e)
        {
            SetInpFlag(2);
        }

        private void btnAtHomeX_Click(object sender, EventArgs e)
        {
            SetAtHomeFlag(1);
        }

        private void btnAtHomeY_Click(object sender, EventArgs e)
        {
            SetAtHomeFlag(2);
        }

        private void btnIoTest_Click(object sender, EventArgs e)
        {
            UInt32 value1 = 0;
            UInt32.TryParse(qqTextBox1.Text, out value1);

            UInt32 value2 = 0;
            UInt32.TryParse(qqTextBox2.Text, out value2);

            UInt32 value3 = 0;
            UInt32.TryParse(qqTextBox3.Text, out value3);

            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_IOTEST;
            stSend.sendMsg = new byte[12];
            stSend.sendMsg[0] = (byte)(value1 & 0xff);
            stSend.sendMsg[1] = (byte)(value1 >> 8 & 0xff);
            stSend.sendMsg[2] = (byte)(value1 >> 16 & 0xff);
            stSend.sendMsg[3] = (byte)(value1 >> 24 & 0xff);
            stSend.sendMsg[4] = (byte)(value2 & 0xff);
            stSend.sendMsg[5] = (byte)(value2 >> 8 & 0xff);
            stSend.sendMsg[6] = (byte)(value2 >> 16 & 0xff);
            stSend.sendMsg[7] = (byte)(value2 >> 24 & 0xff);
            stSend.sendMsg[8] = (byte)(value3 & 0xff);
            stSend.sendMsg[9] = (byte)(value3 >> 8 & 0xff);
            stSend.sendMsg[10] = (byte)(value3 >> 16 & 0xff);
            stSend.sendMsg[11] = (byte)(value3 >> 24 & 0xff);
            EnQueueSend(stSend);
        }

        private void SetOrigin()
        {
            // 在mDockSetParameter中保存原点设置信息，并显示到文本框中
            CGumController.mDockSetParameter.originXSet = CGumController.mMashineInfo.originX + CGumController.mMashineInfo.x;
            CGumController.mDockSetParameter.originYSet = CGumController.mMashineInfo.originY + CGumController.mMashineInfo.y;
            CGumController.mDockSetParameter.ShowParameter();

            // 发送原点设置指令
            SendSetOriginCmd(CGumController.mDockSetParameter.originXSet, CGumController.mDockSetParameter.originYSet);
        }

        private void SendSetOriginCmd(int coordinateX, int coordinateY)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_SET_ORIGIN;
            stSend.sendMsg = new byte[8];
            stSend.sendMsg[0] = (byte)(coordinateX & 0xff);
            stSend.sendMsg[1] = (byte)(coordinateX >> 8 & 0xff);
            stSend.sendMsg[2] = (byte)(coordinateX >> 16 & 0xff);
            stSend.sendMsg[3] = (byte)(coordinateX >> 24 & 0xff);

            stSend.sendMsg[4] = (byte)(coordinateY & 0xff);
            stSend.sendMsg[5] = (byte)(coordinateY >> 8 & 0xff);
            stSend.sendMsg[6] = (byte)(coordinateY >> 16 & 0xff);
            stSend.sendMsg[7] = (byte)(coordinateY >> 24 & 0xff);
            CGumController.mDockSerialProtocol.EnQueueSend(stSend);
        }

        private void btnSetOringin_Click(object sender, EventArgs e)
        {
            SetOrigin();
        }

        private void btnMoveToOrigin_Click(object sender, EventArgs e)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_MOVETO_ORIGIN;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = 1;
            EnQueueSend(stSend);
        }

        private void btnLifeTestOn_Click(object sender, EventArgs e)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_LIFETEST;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = 1;
            EnQueueSend(stSend);
        }

        private void btnLifeTestOff_Click(object sender, EventArgs e)
        {
            ET_GPES_SENDMSG stSend = new ET_GPES_SENDMSG();
            stSend.CLA = (byte)CLA.CLA_CTRL;
            stSend.INS = (byte)INS.CMD_LIFETEST;
            stSend.sendMsg = new byte[1];
            stSend.sendMsg[0] = 0;
            EnQueueSend(stSend);
        }
    }
}
